const http = require('http');

console.log('📅 测试日期查询和任务数据...\n');

// 测试基础任务数据
function testTaskData() {
  return new Promise((resolve, reject) => {
    const options = {
      hostname: 'localhost',
      port: 5000,
      path: '/api/tasks',
      method: 'GET'
    };
    
    console.log('📋 获取所有任务数据...');
    
    const req = http.request(options, (res) => {
      let body = '';
      
      res.on('data', (chunk) => {
        body += chunk;
      });
      
      res.on('end', () => {
        try {
          const tasks = JSON.parse(body);
          console.log(`✅ 任务总数: ${tasks.length}`);
          
          // 分析任务创建日期
          const today = new Date().toISOString().split('T')[0];
          console.log(`📅 今天日期: ${today}`);
          
          console.log('\n📊 任务详情分析:');
          tasks.forEach((task, index) => {
            const createdDate = new Date(task.createdAt).toISOString().split('T')[0];
            const updatedDate = new Date(task.updatedAt).toISOString().split('T')[0];
            
            console.log(`${index + 1}. ${task.title}`);
            console.log(`   创建时间: ${task.createdAt} -> 日期: ${createdDate}`);
            console.log(`   更新时间: ${task.updatedAt} -> 日期: ${updatedDate}`);
            console.log(`   完成状态: ${task.completed ? '✅' : '❌'}`);
            console.log(`   是否今天创建: ${createdDate === today ? '✅' : '❌'}`);
            console.log('   ─'.repeat(50));
          });
          
          // 统计今天创建的任务
          const todayCreated = tasks.filter(task => {
            const createdDate = new Date(task.createdAt).toISOString().split('T')[0];
            return createdDate === today;
          });
          
          const todayCompleted = tasks.filter(task => {
            const updatedDate = new Date(task.updatedAt).toISOString().split('T')[0];
            return updatedDate === today && task.completed;
          });
          
          console.log(`\n📈 统计结果:`);
          console.log(`今天创建的任务: ${todayCreated.length}`);
          console.log(`今天完成的任务: ${todayCompleted.length}`);
          
          resolve({
            total: tasks.length,
            todayCreated: todayCreated.length,
            todayCompleted: todayCompleted.length,
            tasks: tasks
          });
          
        } catch (error) {
          console.log('❌ JSON解析失败:', error.message);
          reject(error);
        }
      });
    });
    
    req.on('error', (error) => {
      console.log('❌ 请求失败:', error.message);
      reject(error);
    });
    
    req.end();
  });
}

// 测试时间趋势API
function testTimeTrends() {
  return new Promise((resolve, reject) => {
    const options = {
      hostname: 'localhost',
      port: 5000,
      path: '/api/stats/time-trends?days=7',
      method: 'GET'
    };
    
    console.log('\n📊 测试时间趋势API...');
    
    const req = http.request(options, (res) => {
      let body = '';
      
      res.on('data', (chunk) => {
        body += chunk;
      });
      
      res.on('end', () => {
        console.log(`状态码: ${res.statusCode}`);
        console.log(`响应体: ${body}`);
        
        if (res.statusCode === 200) {
          try {
            const response = JSON.parse(body);
            console.log('✅ 时间趋势API响应成功');
            console.log('趋势数据:', JSON.stringify(response.data, null, 2));
          } catch (error) {
            console.log('❌ JSON解析失败:', error.message);
          }
        } else {
          console.log('❌ API调用失败');
        }
        
        resolve();
      });
    });
    
    req.on('error', (error) => {
      console.log('❌ 请求失败:', error.message);
      reject(error);
    });
    
    req.end();
  });
}

// 运行测试
async function runTests() {
  try {
    await testTaskData();
    await testTimeTrends();
    console.log('\n🎉 日期查询测试完成!');
  } catch (error) {
    console.error('❌ 测试失败:', error);
  }
}

runTests();
